package cn.iocoder.yudao.module.fengdong.smartelderly.service.useroldman;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.useroldman.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.useroldman.UserOldmanDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.useroldman.UserOldmanConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.useroldman.UserOldmanMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.fengdong.smartelderly.utils.IdentityCardUtils.verifyIdentityCard;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 用户关联老人 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class UserOldmanServiceImpl implements UserOldmanService {

    @Resource
    private UserOldmanMapper userOldmanMapper;

    @Override
    public Long createUserOldman(UserOldmanCreateReqVO createReqVO) {
        if (StringUtil.isNotBlank(createReqVO.getIdentityCard())){
            verifyIdentityCard(createReqVO.getIdentityCard());
        }
        //一个用户绑定老人身份证不能重复
        if (userOldmanMapper.selectCountByIdentityCard(createReqVO.getIdentityCard())>0) {
            throw exception(USER_OLDMAN_EXISTS);
        }
        // 插入
        UserOldmanDO userOldman = UserOldmanConvert.INSTANCE.convert(createReqVO);
        userOldmanMapper.insert(userOldman);
        // 返回
        return userOldman.getId();
    }

    @Override
    public void updateUserOldman(UserOldmanUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserOldmanExists(updateReqVO.getId());
        // 更新
        UserOldmanDO updateObj = UserOldmanConvert.INSTANCE.convert(updateReqVO);
        userOldmanMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserOldman(Long id) {
        // 校验存在
        validateUserOldmanExists(id);
        // 删除
        userOldmanMapper.deleteById(id);
    }

    private void validateUserOldmanExists(Long id) {
        if (userOldmanMapper.selectById(id) == null) {
            throw exception(USER_OLDMAN_NOT_EXISTS);
        }
    }

    @Override
    public UserOldmanDO getUserOldman(Long id) {
        return userOldmanMapper.selectById(id);
    }

    @Override
    public List<UserOldmanDO> getUserOldmanList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return userOldmanMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserOldmanDO> getUserOldmanPage(UserOldmanPageReqVO pageReqVO) {
        return userOldmanMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserOldmanDO> getUserOldmanList(UserOldmanExportReqVO exportReqVO) {
        return userOldmanMapper.selectList(exportReqVO);
    }

    @Override
    public List<UserOldmanDO> getother(UserOldmanReqVO pageVO) {
        return userOldmanMapper.selectList( new LambdaQueryWrapperX<UserOldmanDO>()
                .eq(UserOldmanDO::getIdentityCard, pageVO.getIdentityCard())
                .orderByDesc(UserOldmanDO::getId));
    }

}
